﻿using NPOI.XWPF.UserModel;
using System.IO;
using System;

namespace Ava.Web.ViewModels
{
    public partial class MainWindowViewModel : ViewModelBase
    {
        public MainWindowViewModel()
        {
            CreateWord();

        }
#pragma warning disable CA1822 // Mark members as static
        public string Greeting => "Welcome to Avalonia!";
#pragma warning restore CA1822 // Mark members as static


        public void CreateWord()
        {

            string currentDate = DateTime.Now.ToString("yyyyMMdd");
            string checkTime = DateTime.Now.ToString("yyyy年MM月dd日");//检查时间

            string workFileName = checkTime + "追逐时光企业员工培训考核统计记录表";
            string fileName = string.Format("{0}.docx", workFileName, System.Text.Encoding.UTF8);

            //TODO:使用FileStream文件流来写入数据（传入参数为：文件所在路径，对文件的操作方式，对文件内数据的操作）
            //通过使用文件流，创建文件流对象，向文件流中写入内容，并保存为Word文档格式
            using (var stream = new FileStream(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName), FileMode.Create, FileAccess.Write))
            {
                //创建document文档对象对象实例
                XWPFDocument document = new();

                WordHelper helper = new WordHelper();
                /**
                 *这里我通过设置公共的Word文档中SetParagraph（段落）实例创建和段落样式格式设置，大大减少了代码的冗余，
                 * 避免每使用一个段落而去创建一次段落实例和设置段落的基本样式
                 *(如下，ParagraphInstanceSetting为段落实例创建和样式设置，后面索引表示为当前是第几行段落,索引从0开始)
                 */
                //文本标题
                document.SetParagraph(helper.ParagraphInstanceSetting(document, workFileName, true, 19, "宋体", ParagraphAlignment.CENTER), 0);

                document.SetParagraph(helper.ParagraphInstanceSetting(document, "登记机关：企业员工监督检查机构", false, 14, "宋体", ParagraphAlignment.LEFT), 1);


                //向文档流中写入内容，生成word
                document.Write(stream);



            }

        }
    }
}
